------------------------------------------------------------------------------------------------------------------------------------------------

local function OnAttached(inst,target) -- 玩家得到 debuff 的瞬间。 穿越洞穴、重新进存档 也会执行。
    inst.entity:SetParent(target.entity)
    -- inst.Network:SetClassifiedTarget(target)
    inst.Transform:SetPosition(0,0,0)
    inst.player = target
    local player = target
    -----------------------------------------------------
    ---
        inst:DoTaskInTime(0,function()
            player.components.loramia_com_spell_controller:NetSetData("energy_saving_mode",true)
            player:PushEvent("loramia_event.energy_saving_mode_switched")
            player:RemoveTag("fastbuilder")
            player.components.loramia_com_health_hook:Set(inst,function(amount, overtime, cause,...)
                if amount < 0 and cause == "hunger" then
                    return 0
                end
                return amount
            end)
            if player.components.loramia_com_talent_tree:IsUnlocked("hyper_charging") then
                player.components.hunger.burnratemodifiers:SetModifier(inst,0)
            end
        end)
    -----------------------------------------------------
    ---
        inst:ListenForEvent("onremove",function()
            player.components.loramia_com_spell_controller:NetSetData("energy_saving_mode",false)
            player:PushEvent("loramia_event.energy_saving_mode_switched")
            player:AddTag("fastbuilder")
        end)
    -----------------------------------------------------
end

local function OnDetached(inst) -- 被外部命令  inst:RemoveDebuff 移除debuff 的时候 执行
    -- local player = inst.player
    -- if player then
    --     player.components.loramia_com_spell_controller:NetSetData("energy_saving_mode",false)
    -- end
    inst:Remove()
end

local function OnUpdate(inst)
    local player = inst.player

end

local function ExtendDebuff(inst)
    -- inst.countdown = 3 + (inst._level:value() < CONTROL_LEVEL and EXTEND_TICKS or math.floor(TUNING.STALKER_MINDCONTROL_DURATION / FRAMES + .5))
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()
    inst.entity:AddTransform()

    inst:AddTag("CLASSIFIED")



    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff.keepondespawn = true -- 是否保持debuff 到下次登陆
    -- inst.components.debuff:SetDetachedFn(inst.Remove)
    inst.components.debuff:SetDetachedFn(OnDetached)
    -- inst.components.debuff:SetExtendedFn(ExtendDebuff)
    -- ExtendDebuff(inst)



    return inst
end

return Prefab("loramia_debuff_energy_saving_mode", fn)
